package com.estar.integaration.Parser;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.estar.integaration.Enum.Group;
import com.estar.integaration.Util.Util;
import com.estar.integaration.entity.Material;
import com.estar.integaration.entity.moneyEntity.MaterialMoney;

import java.io.*;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Author: YJL
 * Date: 2021/12/16 22:54
 * 库存账龄分析表
 **/

public class MaterialMoneyParse {

  public List<MaterialMoney> parseExcel(String excelAddress) {

    List<MaterialMoney> materialMoneyLinkedList = new LinkedList<>();

    ExcelReader excelReader = null;
    List<List<Object>> content = null;

    try(InputStream inputStream = new FileInputStream(new File(excelAddress))){
      excelReader = ExcelUtil.getReader(inputStream,0);
      content = excelReader.read(1,excelReader.getRowCount());
      for(List<Object> list : content){
        if(list.get(0) == null)
          continue;
        String materialCode = String.valueOf(list.get(0));
        String materialName = String.valueOf(list.get(1));
        String spec = String.valueOf(list.get(2)).equals("null") ? "" : String.valueOf(list.get(2));
        String unit = String.valueOf(list.get(7));
        String quantity = String.valueOf(list.get(8));
        List<Double> moneyList = getAllMoney(list);//获得四个钱
        if(moneyList.size() == 0)
          continue;
        //初始化materialMoney
        MaterialMoney materialMoney = new MaterialMoney(Util.getUUID(),materialCode,moneyList.get(0),moneyList.get(1),moneyList.get(2),moneyList.get(3),moneyList.get(4), null);
        materialMoney.setMaterialName(materialName);
        materialMoney.setSpec(spec);
        materialMoney.setUnit(unit);
        materialMoney.setQuantity(quantity);
        if(list.size() >= 19){
          Object o = list.get(18);
          if(o != null){
            if(!String.valueOf(o).equals("")){
              materialMoney.setBrand(String.valueOf(list.get(17)));//品牌暂时为空
            }
          }
        }
        materialMoneyLinkedList.add(materialMoney);
      }
      return materialMoneyLinkedList;

    } catch (FileNotFoundException e) {

      e.printStackTrace();

    } catch (IOException e) {

      e.printStackTrace();

    }

    return null;
  }


  public HashMap<String,MaterialMoney> getMCodeMM(List<MaterialMoney> materialMoneyList){
    HashMap<String,MaterialMoney> mCodeMMHashMap = new HashMap<>();
    for(MaterialMoney materialMoney : materialMoneyList){
      if(mCodeMMHashMap.containsKey(materialMoney.getBaseMaterialCode())){
        MaterialMoney hashMaterialMoney = mCodeMMHashMap.get(materialMoney.getBaseMaterialCode());
        MaterialMoney tmpMaterial = addTwoMaterialMoney(hashMaterialMoney,materialMoney);
        mCodeMMHashMap.put(materialMoney.getBaseMaterialCode(),tmpMaterial);
      }else {
        mCodeMMHashMap.put(materialMoney.getBaseMaterialCode(),materialMoney);
      }
    }

    return mCodeMMHashMap;
  }


  private double getMoneyfromString(List list,int index){
    String strNum = String.valueOf(list.get(index));
    if(strNum == null || strNum.equals("") || strNum.equals("null")){
      return 0;
    }else {
      return Double.parseDouble(strNum);
    }
  }

  private List<Double> getAllMoney(List<Object> list){
    List<Double> moneyList = new LinkedList<>();
    if(list == null){
      return moneyList;
    }

    int maxLength = list.size();//最大长度

    if(maxLength < 10){//没有参考金额抛弃
      return moneyList;
    }else if(maxLength >= 10 && maxLength < 13){
      if(getMoneyfromString(list,9) == 0)
        return moneyList;
      Double allMoney = getMoneyfromString(list,9);
      Double money1 = getMoneyfromString(list,11);
      moneyList.add(allMoney);
      moneyList.add(money1);
      moneyList.add(0.0);
      moneyList.add(0.0);
      moneyList.add(0.0);
    }else if(maxLength >= 13 && maxLength < 15){
      if(getMoneyfromString(list,9) == 0)
        return moneyList;
      Double allMoney = getMoneyfromString(list, 9);
      Double money1 = getMoneyfromString(list,11);
      Double money2 = getMoneyfromString(list,13);
      moneyList.add(allMoney);
      moneyList.add(money1);
      moneyList.add(money2);
      moneyList.add(0.0);
      moneyList.add(0.0);
    }else if(maxLength >= 15  && maxLength < 17){
      if(getMoneyfromString(list,9) == 0)
        return moneyList;
      Double allMoney = getMoneyfromString(list,9);
      Double money1 = getMoneyfromString(list,11);
      Double money2 = getMoneyfromString(list,13);
      Double money3 = getMoneyfromString(list,15);
      moneyList.add(allMoney);
      moneyList.add(money1);
      moneyList.add(money2);
      moneyList.add(money3);
      moneyList.add(0.0);
    }else if(maxLength >= 17 && maxLength < 19){
      if(getMoneyfromString(list,9) == 0)
        return moneyList;
      Double allMoney = getMoneyfromString(list,9);
      Double money1 = getMoneyfromString(list,11);
      Double money2 = getMoneyfromString(list,13);
      Double money3 = getMoneyfromString(list,15);
      Double money4 = getMoneyfromString(list,17);
      moneyList.add(allMoney);
      moneyList.add(money1);
      moneyList.add(money2);
      moneyList.add(money3);
      moneyList.add(money4);
    }
    return moneyList;
  }

  /**
   * 处理Hash中出现的重复的物料
   * @param materialMoney1
   * @param materialMoney2
   * @return
   */
  public MaterialMoney addTwoMaterialMoney(MaterialMoney materialMoney1, MaterialMoney materialMoney2){
     materialMoney1.setAllMoney(materialMoney1.getAllMoney() + materialMoney2.getAllMoney());
     materialMoney1.setStepOneMoney(materialMoney1.getStepOneMoney() + materialMoney2.getStepOneMoney());
     materialMoney1.setStepTwoMoney(materialMoney1.getStepTwoMoney() + materialMoney2.getStepTwoMoney());
     materialMoney1.setStepThreeMoney(materialMoney1.getStepThreeMoney() + materialMoney2.getStepThreeMoney());
     materialMoney1.setStepFourMoney(materialMoney1.getStepFourMoney() + materialMoney2.getStepFourMoney());

     return materialMoney1;
  }




}
